<?php
require_once('Zend/Controller/Action.php');

class WebBaseController extends Zend_Controller_Action {
	/**
	 * @var CurrentUser
	 */
	protected $_currentUser = null;
	
	
	public function init() {
		parent::init();
		
		$this->view->assign('currentUser', $this->getCurrentUser());
		
		$existSessionId = $this->getRequest()->getCookie(SiteConfig::USER_COOKIE_NAME);
		if (empty($existSessionId)) return ;
		$existSession = $this->getUserSessionService()->getSession($existSessionId);
		if (empty($existSession)) return ;

		$user = $this->getUserService()->get($existSession['user_id']);
		if (!$user) {
			$this->getUserSessionService()->deleteSession($existSessionId);
			return ;
		}
		$this->getCurrentUser()->setUser($user + array('session' => $existSession));
		
		setcookie(SiteConfig::USER_COOKIE_NAME, $existSessionId, time() + SiteConfig::USER_COOKIE_EXPIRE, '/', null, false, true);
		
		$this->view->assign('currentUser', $this->getCurrentUser());
	}
	
	protected function showNotice($message, $jumpUrl = '', $viewScript = 'notice') {
		$second = 3;
		if ("" == $jumpUrl) $jumpUrl = "javascript:history.back();";
		
		$this->view->assign('jumpurl', $jumpUrl);
		$this->view->assign('message', $message);
		$this->view->headMeta()->appendHttpEquiv("refresh", "{$second};url={$jumpUrl}");
		$this->view->headTitle()->prepend('提示信息');
		$this->render($viewScript, null, true);
	}
	
	protected function getInnerUrl($urlSettings = array()) {
		if (!isset($urlSettings['module'])) $urlSettings['module'] = $this->getRequest()->getModuleName();
		if (!isset($urlSettings['controller'])) $urlSettings['controller'] = $this->getRequest()->getControllerName();
		return $this->view->url($urlSettings, 'default', true);
	}
	
	protected function getSiteBaseUrl() {
		$url = $this->getRequest()->getScheme() . "://";
		$url .= $this->getRequest()->getHttpHost();
		$url .= $this->getRequest()->getBasePath();
		return $url;
	}
	
	/**
	 * @return CurrentUser
	 */
	protected function getCurrentUser() {
		if (!$this->_currentUser) {
			$this->_currentUser = CurrentUser::getInstance();
		}
		return $this->_currentUser;
	}
	
	
	
	/**
	 * @return UserService
	 */
	private function getUserService() {
		require_once 'Models/User/Service/UserServiceFactory.php';
		return UserServiceFactory::getInstance()->createUserService();
	}
	
	/**
	 * @return UserSessionService
	 */
	private function getUserSessionService() {
		require_once 'Models/User/Service/UserServiceFactory.php';
		return UserServiceFactory::getInstance()->createUserSessionService();
	}
}

class CurrentUser {
	private static $instance;
	private $_user = null;
	
	private function __construct() {
	}
	
	/**
	 * @return CurrentUser
	 */
	public static function getInstance() {
		if (!self::$instance) self::$instance = new self();
		return self::$instance;
	}
	
	public function setUser($user) {
		$this->_user = $user;
		return $this;
	}
	
	public function getId() {
		return $this->getInfo('id');
	}
	
	public function getName() {
		return $this->getInfo('name');
	}
	
	public function getEmail() {
		return $this->getInfo('email');
	}
	
	public function isLogin() {
		return (bool) $this->getId();
	}
	
	public function getInfo($key = null, $default = null) {
		if (null === $key) return $this->_user;
		return isset($this->_user[$key]) ? $this->_user[$key] : $default;
	}
}
